<!-- 平台账号變更申请 -->
<template>
	<view class="page">
		<u--form
			labelPosition="top"
			:model="model"
			ref="ErpOmcProjectApplyForm"
			:rules="rules"
			labelWidth="auto"
			:labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}"
		>
			<view class="basic-style">
				<u-form-item
					label="单据编号"
					prop="ptzhbm"
					:borderBottom="false"
					required
				>
					<u--input
						disabled
						placeholder="请输入单据编号"
						v-model="model.ptzhbm"
					></u--input>
				</u-form-item>
				<u-form-item
					label="申请时间"
					prop="createTime"
					:borderBottom="false"
					required
				>
					<u--input
						v-model="model.createTime"
						disabled
						placeholder="请选择申请时间"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				<u-form-item
					label="申请人"
					prop="proposer"
					:borderBottom="false"
					required
				>
					<h-select-person-input
						style="width: 100%"
						disabled
						v-model="model.proposer"
						placeholder="请选择申请人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.proposer"
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="申请部门"
					prop="proposerDeptId"
					:borderBottom="false"
					required
				>
					<h-select-depart-input
						disabled
					  v-model="model.proposerDeptId"
					  :departName="model.proposerDeptText"
					  placeholder="请选择部门"
					  border="surround"
					  style="width: 100%"
					  suffixIcon="arrow-down-fill"
					  :suffixIconStyle="{ color: '#B2B2B2' }"
					  :title.sync="model.proposerDeptText"
					  @detail="ontenderDeptDetail"
					></h-select-depart-input>
				</u-form-item>
				<view class="basic-title-center">
					<u-line :length="120"></u-line>
					<view class="">基本信息</view>
					<u-line :length="120"></u-line>
				</view>
				<!-- v-if="sfbg" -->
				<u-form-item label="变更类型" prop="changeStatus" required>
				  <h-select-input-tag
				    class="w-100"
				    v-model="model.changeStatus"
				    objectField="model.changeStatus"
				    placeholder="请选择变更类型"
				    border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
				    dictName="platform_change_status"
				  ></h-select-input-tag>
				</u-form-item>
				<u-form-item
					label="平台编号"
					prop="plfId"
					:borderBottom="false"
					required
				>
					<h-select-platform-account-input
						v-model="model.plfId"
						style="width: 100%"
						placeholder="请选择平台编号"
						objectField="model.plfId"
						:dataType="'emp'"
						dataIsolation="bgr"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						@input="ptmcChange"
					></h-select-platform-account-input>
				</u-form-item>
				<u-form-item
					label="平台名称"
					prop="ptmc"
					:borderBottom="false"
					required
				>
					<u--input
						:disabled="isCancel"
						placeholder="请输入平台名称"
						v-model="model.ptmc"
						@change="changeTitle"
					></u--input>
				</u-form-item>
				<u-form-item
					label="网址"
					prop="wz"
					:borderBottom="false"
					required
				>
					<u--input
						:disabled="isCancel"
						placeholder="请输入网址"
						v-model="model.wz"
					></u--input>
				</u-form-item>
				<u-form-item
					label="保管人"
					prop="bgr"
					:borderBottom="false"
					required
				>
					<h-select-person-input
						style="width: 100%"
						:disabled="isCancel"
						v-model="model.bgr"
						placeholder="请选择保管人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.bgr"
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="会员单位"
					prop="hydw"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入会员单位"
						:disabled="isCancel"
						v-model="model.hydw"
					></u--input>
				</u-form-item>
				
				<u-form-item
					label="登录名"
					prop="dlm"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入登录名"
						:disabled="isCancel"
						v-model="model.dlm"
					></u--input>
				</u-form-item>
				<u-form-item
					label="密码"
					prop="mm"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入密码"
						:disabled="isCancel"
						v-model="model.mm"
					></u--input>
				</u-form-item>
				<u-form-item
					label="会员等级"
					prop="hydj"
					:borderBottom="false"
				>
					<u--input
						placeholder="请输入会员等级"
						:disabled="isCancel"
						v-model="model.hydj"
					></u--input>
				</u-form-item>
				<u-form-item
					label="会员有效期"
					prop="hyyxqs"
					:borderBottom="false"
					required
				>
					<h-calendar-datetime-select
						style="width: 100%"
						v-model="memberDate"
						:disabled="isCancel"
						@confirm="onCalendarConfirm"
						placeholder="请选择会员有效期"
					></h-calendar-datetime-select>
				</u-form-item>
				<u-form-item
					label="ca锁密码"
					prop="casmm"
					:borderBottom="false"
					required
				>
					<u--input
						placeholder="请输入ca锁密码"
						:disabled="isCancel"
						v-model="model.casmm"
					></u--input>
				</u-form-item>
				<u-form-item
					label="ca锁个数"
					prop="casgs"
					:borderBottom="false"
				>
					<h-number-input placeholder="请输入ca锁个数" :disabled="isCancel" v-model="model.casgs" :digit="0" :min="0"></h-number-input>
				</u-form-item>
				
				<u-form-item
					label="ca锁有效期"
					prop="casyxqs"
					:borderBottom="false"
					required
				>
					<h-calendar-datetime-select
						style="width: 100%"
						:disabled="isCancel"
						v-model="CaDate"
						@confirm="onCalendarConfirmCa"
						placeholder="ca锁有效期"
					></h-calendar-datetime-select>
				</u-form-item>
				
				
				<u-form-item
					label="备注"
					prop="bz"
					:borderBottom="false"
				>
					<u--textarea
						v-model="model.bz"
						:confirmType="null"
						count
						placeholder="请输入备注"
						:maxlength="500"
					></u--textarea>
				</u-form-item>
				
				<u-form-item
					label="平台注册授权联系人"
					prop="platformContacts"
					:borderBottom="false"
				>
					<u--input
						placeholder="请输入平台注册授权联系人"
						:disabled="isCancel"
						v-model="model.platformContacts"
					></u--input>
				</u-form-item>
				<u-form-item
					label="联系方式"
					prop="platformContactsTel"
					:borderBottom="false"
				>
					<u--input
						placeholder="请输入联系方式"
						:disabled="isCancel"
						v-model="model.platformContactsTel"
					></u--input>
				</u-form-item>
			</view>
		</u--form>
		<view class="space-x-1 px-1 btn_position">
			<u-button :disabled="isSubmit" plain type="primary" text="保存" @click="onDrafts"></u-button>
			<u-button :disabled="isSubmit"  type="primary" text="保存并提交" @click="onSubmit" style="margin-left: 40rpx;"></u-button>
		</view>
		<view style="height: 100rpx;"></view>
		<u-toast ref="uToast"></u-toast>
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
	</view>
</template>
<script>
import fileList from '@/pages/synergy/components/fileList.vue'
import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
import http from '@/utils/request'
import {
	generatedFormNumber, // 获取单据编号
	erpOmcPlatformAccountAdd, // 添加
	erpOmcPlatformAccountEdit, // 编辑
	erpOmcPlatformAccountQueryById, // 详情
	erpOmcPlatformAccountFinalUpdateStatus
} from '@/api/index.js'
import moment from 'moment'
import { filterMultiDictText, initDictOptions } from '@/utils/JDictSelectUtil.js'
import {
	BASE_URL
} from '@/utils/baseUrl.js'
import { convertCurrency } from '@/utils/MoneyConvert' // 金额大写转换
import { mapState } from 'vuex'
export default {
	components: {
		fileList,
		ProcessNodeConfiguration
	},
	data() {
		return {
			memberDate: [], // 会员有效期
			CaDate: [], // ca有效期
			sfbg: false,
			isSubmit: false,
			model: {
				ptzhbm: '', //单据编号
				createTime: '', // 申请时间
				proposer: '', // 申请人
				proposerDeptId: '', // 申请部门
				changeStatus: '', // 变更类型
				plfId: '', // 平台编号
				ptmc: '', // 平台名称
				wz: '', // 网址
				bgr: '', // 保管人
				hydw: '', // 会员单位
				dlm: '', // 登录名
				mm: '', // 密码
				hydj: '', // 会员等级
				hyyxqs: '', // 会员有效期始
				hyyxqe: '', // 会员有效期止
				casmm: '', // ca锁密码
				casgs: '', // ca锁个数
				
				casyxqs: '', // ca锁有效期始
				casyxqe: '', // ca锁有效期止
				bz: '', //备注
				platformContacts: '', // 平台注册授权联系人
				platformContactsTel: '', // 联系方式
			},
			id: '', // 详情id值
			rules: {
				ptmc: [
				   { required: true, message: '请输入平台名称!', trigger: ['change','blur'] },
				],
				wz: [
					{ required: true, message: '请输入网址!', trigger: ['change','blur'], },
					{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							return uni.$u.test.url(value);
						},
						message: '请输入正确的网址!',
						// 触发器可以同时用blur和change
						trigger: ['change','blur'],
					}
				],
				
				bgr: [
					{ required: true, message: '请选择保管人!', trigger: ['change','blur'], },
				],
				changeStatus: [
					{ required: true, message: '请选择变更类型!', trigger: ['change','blur'], },
				],
				plfId: [
					{ required: true, message: '请选择平台编号!', trigger: ['change','blur'], },
				],
				hydw: [
					{ required: true,  message: '请输入会员单位!', trigger: ['change','blur'], },
				],
				
				dlm: [
				  { required: true, message: '请输入登录名!', trigger: ['change','blur'], },
				],
				mm: [
				  { required: true, message: '请输入密码!', trigger: ['change','blur'], },
				],
				hyyxqs: [
				  { required: true, message: '请选择会员有效期!', trigger: ['change','blur'], },
				],
				platformContactsTel: [
					{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 返回true表示校验通过，返回false表示不通过
							if (!value) return true
							var reg = /^1[3456789]\d{9}$/
							if (reg.test(value)) {
								return true
							} else {
								return false
							}
							// return uni.$u.test.mobile(value);
						},
						message: '请输入正确的联系方式!',
						trigger: ['change','blur'],
					}
				],
				casmm: [
				  { required: true, message: '请输入ca锁密码!', trigger: ['change','blur'], },
				],
				casyxqs: [
					{ required: true, message: '请选择ca锁有效期', trigger: ['change','blur'] }
				],
			},
			dictOptions: {},
			//代码生成后需手动设置流程编码
			formUrl: 'erp/omc/platformaccount/modules/ErpOmcPlatformAccountForm',
			formUrlMobile: '/pages/synergy/erpOmcPlatformAccountList',
			flowCode: 'dev_erp_omc_platform_account_001',
			beanName: 'erpOmcPlatformAccountServiceImpl',
			processId: '1469208959040917506',
		};
	},
	
	onLoad(options) {
		this.$set(this.model,'lx', 2)
		this.initDictConfig()
		this.id = options.id || ''
		this.sfbg = true
		if (options.id) {
			// 编辑
			this.edit(options.id)
		} else {
			// 添加
			this.add()
		}
	},
	computed: {
	  ...mapState(['userInfo']),
		// 判断是否作废
		isCancel () {
			return this.model.changeStatus === 'invalid'
		}
	},
	methods: {
		initDictConfig() {
		  initDictOptions('erp_employee_file,name,id').then((res) => {
		    if (res.success) {
		      this.$set(this.dictOptions, 'proposer', res.result)
		    }
		  })
		},
		// 编辑
		edit(id) {
			// 获取详情
			this.getDetail(id)
		},
		// 获取详请
		getDetail (id) {
			erpOmcPlatformAccountQueryById({ id }).then(res => {
				if (res.success) {
					this.model = {...res.result};
					this.memberDate = [res.result.hyyxqs, res.result.hyyxqe]
					this.CaDate = [res.result.casyxqs, res.result.casyxqe]
				}
			})
		},
		
		// 获取流程列表
		getProcessList (id) {
			if (this.isSubmit) {
				if(this.model.lx==2){
				    // 修改为变更中
					erpOmcPlatformAccountFinalUpdateStatus({code:this.model.ptzhbm}).then(res=>{
						if(res.code == 200){
							this.$refs.processConfig.showPopup = true
							this.$refs.processConfig.formUrl = this.formUrl
							this.$refs.processConfig.formUrlMobile = this.formUrlMobile
							this.$refs.processConfig.flowCode = this.flowCode
							this.$refs.processConfig.edit(id, this.processId, this.beanName)
						} else {
							this.$refs.uToast.show({
								message: res.message,
								type: 'error',
								complete () {
									this.isSubmit = false
									if (getCurrentPages().length > 1) {
										uni.navigateBack()
									} else {
										uni.reLaunch({
											url: '/pages/home/home'
										})
									}
									
								}
							})
						}
					})
				}else {
					this.$refs.processConfig.showPopup = true
					this.$refs.processConfig.formUrl = this.formUrl
					this.$refs.processConfig.formUrlMobile = this.formUrlMobile
					this.$refs.processConfig.flowCode = this.flowCode
					this.$refs.processConfig.edit(id, this.processId, this.beanName)
				}
			}
		},
		
		// 投标部门详情
		ontenderDeptDetail(item) {
		  var title = ''
		  if (item && item.length) {
		    item.map((value, index) => {
		      title = value.departName + ',' + title
		    })
		    title = title.substring(0, title.lastIndexOf(','))
		  }
		  this.model.proposerDeptText = title
		  
		},
		
		changeTitle() {
			if (this.id) {
				return false
			}
			this.$set(this.model, 'bpmBizTitle', this.model.ptmc) // 标题
		},
		
		ptmcChange(val) {
			if (!val) {
				return false
			}
		    let data = val
		    this.$set(this.model, 'plfId', val.id)
		    this.$set(this.model, 'ptmc', data.ptmc) // 平台名称
		    this.$set(this.model, 'bpmBizTitle', data.ptmc) // 标题
		    this.$set(this.model, 'hydw', data.hydw) //会员单位
		    this.$set(this.model, 'wz', data.wz) // 网址
		    this.$set(this.model, 'bgr', data.bgr) // 保管人
		    this.$set(this.model, 'dlm', data.dlm) // 登录名
		    this.$set(this.model, 'mm', data.mm) //密码
		    this.$set(this.model, 'hydj', data.hydj) //会员等级
		    this.$set(this.model, 'hyyxqs', data.hyyxqs)//会员有效期始
		    this.$set(this.model, 'hyyxqe', data.hyyxqe)//会员有效期止
			this.memberDate = [this.model.hyyxqs, this.model.hyyxqe]
		    this.$set(this.model, 'casmm', data.casmm)//ca锁密码
		    this.$set(this.model, 'casgs', data.casgs)//ca锁个数
		    this.$set(this.model, 'casyxqs', data.casyxqs)//ca锁有效期始
		    this.$set(this.model, 'casyxqe', data.casyxqe)//ca锁有效期止
			this.CaDate = [this.model.casyxqs, this.model.casyxqe]
		    this.$set(this.model, 'bz', data.bz) // 备注
		    this.$set(this.model, 'ptzhbm', data.ptzhbm)
		    this.$set(this.model, 'platformContacts', data.platformContacts)
		    this.$set(this.model, 'platformContactsTel', data.platformContactsTel)
		},
		
		// 添加
		add() {
			this.getGeneratedFormNumber()
			this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 申请时间
			this.model.proposer = uni.getStorageSync('userInfo').empId; //申请人
			this.model.proposerDeptId = uni.getStorageSync('userInfo').deptId; //申请部门
		},
		// 获取单据编号
		getGeneratedFormNumber() {
			generatedFormNumber({ prefix: 'PTZH', subSystemCode: '05' }).then(res => {
				if (res.success) {
					this.$set(this.model, 'ptzhbm', res.result)
					// this.model.bpmBizTitle = this.userInfo?.employee?.name + '-' + this.model.ptzhbm //标题
				}
			})
		},
		
		// 选择会员有效期
		onCalendarConfirm (val) {
			
			if (val && val.length && val.length == 2) {
				this.$set(this.model, 'hyyxqs', val[0])
				this.$set(this.model, 'hyyxqe', val[val.length - 1])
				this.memberDate = [this.model.hyyxqs, this.model.hyyxqe]
			}
		},
		
		// 选择ca锁有效期
		onCalendarConfirmCa (val) {
			if (val && val.length && val.length == 2) {
				this.$set(this.model, 'casyxqs', val[0])
				this.$set(this.model, 'casyxqe', val[val.length - 1])
				this.CaDate = [this.model.casyxqs, this.model.casyxqe]
			}
		},
		
		// 点击提交
		async onSubmit () {
			this.model.bpmBizTitle = await (this.model.ptmc ? this.model.ptmc + '-' : '') + filterMultiDictText(this.dictOptions['proposer'], this.model.proposer + '') + '-' + this.model.ptzhbm
			await this.$refs.ErpOmcProjectApplyForm.validate()
			this.isSubmit = true
			let res = null
			let type = ''
			if (this.model.lx == '2') {
				this.model.status = this.model.changeStatus == 'invalid' ? '3' : '2'
			}
			if (!this.id) {
				res = await erpOmcPlatformAccountAdd(this.model)
			} else {
				res = await erpOmcPlatformAccountEdit(this.model)
			}
			
			if(res.success){
				type='default'
				if (this.id) {
					await this.getProcessList(this.id)
				} else {
					// 判断添加的时候是否有返回id值
					if (res.result) {
						await this.getProcessList(res.result)
					}
				}
			}else{
				type='error'
			}
			
			this.$refs.uToast.show({
				message: res.message,
				type
			})
		},
		
		onDrafts() {
			var that = this
			this.model.bpmBizTitle = (this.model.ptmc ? this.model.ptmc + '-' : '') + filterMultiDictText(this.dictOptions['proposer'], this.model.proposer + '') + '-' + this.model.ptzhbm
			this.isSubmit = true
			let httpurl = '';
			if (!this.model.id) {
				httpurl = erpOmcPlatformAccountAdd;
			} else {
				httpurl = erpOmcPlatformAccountEdit;
			}
			if (this.model.lx == '2') {
				this.model.status = this.model.changeStatus == 'invalid' ? '3' : '2'
			}
			httpurl(this.model).then(res => {
				if (res.success) {
					var that = this
					this.$refs.uToast.show({
						message: res.message,
						type: 'default',
						complete () {
							that.isSubmit = false
							if (getCurrentPages().length > 1) {
								uni.navigateBack()
							} else {
								uni.reLaunch({
									url: '/pages/home/home'
								})
							}
							
						}
					})
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		
		// 提交流程成功
		onOk () {
			this.$refs.uToast.show({
				message: '提交成功',
				type: 'success',
				complete () {
					if (getCurrentPages().length > 1) {
						uni.navigateBack()
					} else {
						uni.reLaunch({
							url: '/pages/home/home'
						})
					}
					
				}
			})
		}
	}
};
</script>

<style lang="scss" scoped>
@import url("@/assets/inputStyle.scss");
</style>